package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.R;
import com.hqyj.pojo.UserInfo;
import com.hqyj.dao.UserInfoMapper;
import com.hqyj.service.UserInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.util.JwtUtil;
import com.hqyj.util.MdFive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2023-02-24 09:28:58
 */
@Service
@Slf4j
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    UserInfoMapper userInfoMapper;
    @Autowired
    MdFive mdFive;
    @Override
    public R login(UserInfo userInfo) {
        log.info("-------登录服务-------");
        //验证码用户名
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",userInfo.getUserName());
        UserInfo user = userInfoMapper.selectOne(queryWrapper);
        if(user==null){
            log.info("用户名不正确");
            return R.error("用户名不正确");
        }
        //验证密码
        String pwd = mdFive.getPassword(userInfo.getUserPwd(),user.getUserSalt());
        if(!pwd.equals(user.getUserPwd())) {
            log.info("密码不正确");
            return R.error("密码不正确");
        }
        //----------------生成JWT-------------
        log.info("----jwt生成----");
        //生成jwt的盐值
        String jwtSalt = UUID.randomUUID()+"";
        //生成token
        String token = JwtUtil.createUserToken(user.getUserId(),user.getUserName(),jwtSalt);

        return R.ok("登录成功",200,token);
    }
}
